import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    flag: false,
    employeeList: [],
    searchValue: ""
  },

  lifetimes: {
    attached: function () {
      this.getData();
    }
  },

  pageLifetimes: {
    show: function () {
      wx.getStorage({
        key: "back",
        success: (res) => {
          let back = res.data;
          if (back == 1) {
            this.getData();
          }
          wx.removeStorage({
            key: "back"
          });
        }
      });
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 搜索小工姓名、手机号码
    onSearch() {
      this.getData();
    },
    getData() {
      wx.showLoading({
        title: "获取中..."
      });
      wx.$apis
        .getMarketUser({
          keyword: this.data.searchValue,
          current: 1,
          size: 100,
          companyId: app.globalData.userInfo.companyData.companyId,
          marketRoleId: 3
        })
        .then((res) => {
          wx.stopPullDownRefresh();
          if (res.data) {
            const { records } = res.data;
            this.setData({
              employeeList: records.map((item) => {
                item.roleName = item.marketRoles.length && item.marketRoles[0].marketRoleName;
                return item;
              })
            });
          }
        });
    },
    // 点击跳转修改
    onClick(event) {
      const id = event.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/pages/management/add-worker/index?id=" + id
      });
    },
    onDelete(event) {
      const id = event.currentTarget.dataset.id;
      Dialog.confirm({
        message: "确定删除吗？"
      }).then(() => {
        this.delete(id);
      });
    },
    // 删除亦是解绑
    delete(id) {
      wx.showLoading({
        title: "删除中..."
      });
      wx.$apis
        .deleteMarketUser({
          marketUserIds: [id],
          marketRoleId: 3
        })
        .then((res) => {
          this.getData();
          wx.showToast({
            title: "删除成功",
            icon: "success",
            duration: 2000
          });
        });
    },
    toAddWorker() {
      wx.navigateTo({
        url: "/pages/management/add-worker/index"
      });
    },
    onPullDownRefresh() {
      this.getData();
    },
    onReachBottom() {
      // this.getData()
    }
  }
});
